package cn.harry.cabinet.server.dto.hardware

import cn.harry.cabinet.model.entity.HardwareDevice
import cn.harry.cabinet.model.enums.DeviceType
import cn.harry.cabinet.repository.HardwareDeviceRepository
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.Serializable

/**
 * 硬件设备响应DTO
 */
@Serializable
@InternalSerializationApi
data class HardwareDeviceResponse(
    val deviceType: String,
    val deviceName: String,
    val serialPort: String,
    val baudRate: Int,
    val dataBits: Int,
    val stopBits: Int,
    val parity: Int,
    val enabled: Boolean,
    val createTime: Long,
    val updateTime: Long
) {
    companion object {
        fun fromHardwareDevice(device: HardwareDevice): HardwareDeviceResponse {
            return HardwareDeviceResponse(
                deviceType = device.deviceType.name,
                deviceName = device.deviceName,
                serialPort = device.serialPort,
                baudRate = device.baudRate,
                dataBits = device.dataBits,
                stopBits = device.stopBits,
                parity = device.parity,
                enabled = device.enabled,
                createTime = device.createTime.time,
                updateTime = device.updateTime.time
            )
        }
        
        fun fromHardwareDeviceList(devices: List<HardwareDevice>): List<HardwareDeviceResponse> {
            return devices.map { fromHardwareDevice(it) }
        }
    }
}

/**
 * 硬件设备表单请求DTO
 */
@Serializable
@InternalSerializationApi
data class HardwareDeviceFormRequest(
    val deviceType: String,
    val deviceName: String,
    val serialPort: String,
    val baudRate: Int,
    val dataBits: Int = 8,
    val stopBits: Int = 1,
    val parity: Int = 0,
    val enabled: Boolean = true
) {
    /**
     * 验证请求数据
     */
    fun isValid(): Boolean {
        return deviceType.isNotBlank() &&
               deviceName.isNotBlank() &&
               serialPort.isNotBlank() &&
               baudRate > 0 &&
               dataBits in 5..8 &&
               stopBits in 1..2 &&
               parity in 0..2
    }
    
    /**
     * 获取验证错误信息
     */
    fun getValidationErrors(): List<String> {
        val errors = mutableListOf<String>()
        
        if (deviceName.isBlank()) {
            errors.add("设备名称不能为空")
        }
        
        if (deviceType.isBlank()) {
            errors.add("设备类型不能为空")
        } else if (deviceType != "LOCK_CONTROLLER" && deviceType != "CARD_READER") {
            errors.add("设备类型必须是 LOCK_CONTROLLER 或 CARD_READER")
        }
        
        if (serialPort.isBlank()) {
            errors.add("串口路径不能为空")
        }
        
        if (baudRate <= 0) {
            errors.add("波特率必须大于0")
        }
        
        if (dataBits !in 5..8) {
            errors.add("数据位必须在5-8之间")
        }
        
        if (stopBits !in 1..2) {
            errors.add("停止位必须是1或2")
        }
        
        if (parity !in 0..2) {
            errors.add("校验类型必须是0(无校验)、1(奇校验)或2(偶校验)")
        }
        
        return errors
    }
    
    
    /**
     * 转换为HardwareDevice实体
     */
    fun toHardwareDevice(): HardwareDevice {
        return HardwareDevice(
            deviceType = DeviceType.valueOf(deviceType),
            deviceName = deviceName,
            serialPort = serialPort,
            baudRate = baudRate,
            dataBits = dataBits,
            stopBits = stopBits,
            parity = parity,
            enabled = enabled
        )
    }
}

/**
 * 更新设备启用状态请求DTO
 */
@Serializable
@InternalSerializationApi
data class UpdateDeviceEnabledRequest(
    val enabled: Boolean
)

/**
 * 更新设备串口配置请求DTO
 */
@Serializable
@InternalSerializationApi
data class UpdateSerialConfigRequest(
    val serialPort: String,
    val baudRate: Int,
    val dataBits: Int = 8,
    val stopBits: Int = 1,
    val parity: Int = 0
) {
    fun isValid(): Boolean {
        return serialPort.isNotBlank() &&
               baudRate > 0 &&
               dataBits in 5..8 &&
               stopBits in 1..2 &&
               parity in 0..2
    }
    
    fun getValidationErrors(): List<String> {
        val errors = mutableListOf<String>()
        
        if (serialPort.isBlank()) {
            errors.add("串口路径不能为空")
        }
        
        if (baudRate <= 0) {
            errors.add("波特率必须大于0")
        }
        
        if (dataBits !in 5..8) {
            errors.add("数据位必须在5-8之间")
        }
        
        if (stopBits !in 1..2) {
            errors.add("停止位必须是1或2")
        }
        
        if (parity !in 0..2) {
            errors.add("校验类型必须是0(无校验)、1(奇校验)或2(偶校验)")
        }
        
        return errors
    }
}
